package com.scorpio.baselibrary.ui;

import android.os.Bundle;
import androidx.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import com.scorpio.baselibrary.R;
import com.scorpio.baselibrary.ScreenAdapterTools;
import com.scorpio.baselibrary.utils.BarTextColorUtils;
import com.scorpio.baselibrary.utils.ViewController;

/**
 * created by zhud on 2018/10/25
 */
public abstract class BasicTitleActivity extends BasicActivity implements View.OnClickListener {
    private View basis_statusbar_view, basis_content_layout;
    private FrameLayout basis_content_view, basis_toolbar_view;
    protected ViewController v;
    private int mStatusheight = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BarTextColorUtils.setLightStatusBar(this, false);
        setContentView(R.layout.scorpio_activity_basic);
        initView();
        initTopView();
        v = new ViewController(this, getWindow());
        ScreenAdapterTools.getInstance().loadView(getWindow().getDecorView());
        resetStatusViewHeight();
        onInit(savedInstanceState);
    }

    protected abstract int onCreateLayout();

    protected abstract void onInit(Bundle savedInstanceState);

    //显示返回键
    protected void showBackBtn() {
        v.setGone(R.id.back_btn, true);
    }

    //显示返回键
    protected void showMenuBtn() {
        v.setGone(R.id.menu_btn, true);
    }

    protected void setBasicTitle(int res_id) {
        v.setText(R.id.title_text, res_id);
    }

    protected void setBasicTitle(String str) {
        v.setText(R.id.title_text, str);
    }

    protected void setBasicTitleColor(int color) {
        v.setTextColor(R.id.title_text, color);
    }

    protected ImageView getMenuImageView() {
        return v.getView(R.id.menu_image);
    }

    //初始化布局
    private void initView() {
        basis_content_view = findViewById(R.id.basis_content_view);
        basis_content_layout = findViewById(R.id.basis_content_layout);
        View view = LayoutInflater.from(this).inflate(onCreateLayout(), null, false);
        ViewGroup.LayoutParams layoutParams = basis_content_view.getLayoutParams();
        layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
        layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
        basis_content_view.setLayoutParams(layoutParams);
        basis_content_view.addView(view);
    }

    //初始化默认头布局
    private void initTopView() {
        basis_toolbar_view = findViewById(R.id.basis_toolbar_view);
        View view = LayoutInflater.from(this).inflate(R.layout.scorpio_layout_toolbar, basis_toolbar_view, false);
        view.findViewById(R.id.back_btn).setOnClickListener(this);
        view.findViewById(R.id.menu_btn).setOnClickListener(this);
        basis_toolbar_view.addView(view);
        setTopLayoutParams(view);
    }

    //替换头布局
    protected void setTopView(int topViewlayoutId) {
        View topView = LayoutInflater.from(this).inflate(topViewlayoutId, basis_toolbar_view, false);
        basis_toolbar_view.addView(topView);
        setTopLayoutParams(topView);
    }

    //开始头布局
    private void setTopLayoutParams(final View topView) {
        basis_toolbar_view.post(new Runnable() {
            @Override
            public void run() {
                ViewGroup.LayoutParams layoutParams = basis_toolbar_view.getLayoutParams();
                layoutParams.height = topView.getHeight();
                layoutParams.width = topView.getWidth();
                basis_toolbar_view.setLayoutParams(layoutParams);
            }
        });
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.back_btn) {
            finish();
        }
    }

    //将状态栏高度赋给basis_tatus_view
    private void resetStatusViewHeight() {
        basis_statusbar_view = findViewById(R.id.basis_statusbar_view);
        mStatusheight = BarTextColorUtils.getStatusBarHeight(this);
        if (mStatusheight != -1) {
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mStatusheight);
            basis_statusbar_view.setLayoutParams(params);
        }
    }

    //显示状态栏
    protected void showStatusBar() {
        basis_statusbar_view.setVisibility(View.VISIBLE);
        FrameLayout.LayoutParams fparams = (FrameLayout.LayoutParams) basis_content_layout.getLayoutParams();
        fparams.topMargin = 0;
        basis_content_layout.setLayoutParams(fparams);
    }

    protected void hideStatusBar() {
        basis_statusbar_view.setVisibility(View.GONE);
        FrameLayout.LayoutParams fparams = (FrameLayout.LayoutParams) basis_content_layout.getLayoutParams();
        fparams.topMargin = -mStatusheight;
        basis_content_layout.setLayoutParams(fparams);
    }

    public void showToolbarView() {
        basis_toolbar_view.setVisibility(View.VISIBLE);
    }

    //隐藏头布局
    public void hideToolbarView() {
        basis_toolbar_view.setVisibility(View.GONE);
    }
}
